export default `
uniform float mixNum;
uniform sampler2D colorTexture;//颜色纹理，帧缓存中的一帧当作纹理
uniform sampler2D stcshadow; //
uniform sampler2D videoTexture;//视频纹理
uniform sampler2D depthTexture;//深度纹理
uniform mat4 _shadowMap_matrix;//深度图矩阵 
uniform vec4 shadowMap_lightPositionEC; 
uniform vec4 shadowMap_normalOffsetScaleDistanceMaxDistanceAndDarkness; 
uniform vec4 shadowMap_texelSizeDepthBiasAndNormalShadingSmooth; 
varying vec2 v_textureCoordinates;
// 转换到相机视图坐标系
vec4 toEye(in vec2 uv, in float depth){    
    vec2 xy = vec2((uv.x * 2.0 - 1.0),(uv.y * 2.0 - 1.0));    
    vec4 posInCamera =czm_inverseProjection * vec4(xy, depth, 1.0);    
    posInCamera =posInCamera / posInCamera.w;    
    return posInCamera;
}
// 获取深度
float getDepth(in vec4 depth){    
    float z_window = czm_unpackDepth(depth);    
    z_window = czm_reverseLogDepth(z_window);    
    float n_range = czm_depthRange.near;    
    float f_range = czm_depthRange.far;    
    return (2.0 * z_window - n_range - f_range) / (f_range - n_range);
}
// 深度图uv采样
float _czm_sampleShadowMap(sampler2D shadowMap, vec2 uv){    
    return texture2D(shadowMap, uv).r;
}
// 比较深度值
float _czm_shadowDepthCompare(sampler2D shadowMap, vec2 uv, float depth){    
    return step(depth, _czm_sampleShadowMap(shadowMap, uv));
}
    //是否可见 
float _czm_shadowVisibility(sampler2D shadowMap, czm_shadowParameters shadowParameters){    
    // 深度偏差，比较深度时使用控制精度
    float depthBias = shadowParameters.depthBias;
    //传入的深度值 
    float depth = shadowParameters.depth;
    // 光线方向
    float nDotL = shadowParameters.nDotL;    
    float normalShadingSmooth = shadowParameters.normalShadingSmooth;    
    float darkness = shadowParameters.darkness;    
    vec2 uv = shadowParameters.texCoords;    
    // 如果深度值比深度图中的值大depthBias，则判定为被遮挡
    depth -= depthBias;    
    vec2 texelStepSize = shadowParameters.texelStepSize;    
    float radius = 1.0;    
    float dx0 = -texelStepSize.x * radius;    
    float dy0 = -texelStepSize.y * radius;    
    float dx1 = texelStepSize.x * radius;    
    float dy1 = texelStepSize.y * radius;    
    // 该像素点周围的9个像素求期望
    // float visibility = (    
    //     _czm_shadowDepthCompare(shadowMap, uv, depth)    
    //     +_czm_shadowDepthCompare(shadowMap, uv + vec2(dx0, dy0), depth) +   
    //     _czm_shadowDepthCompare(shadowMap, uv + vec2(0.0, dy0), depth) +   
    //     _czm_shadowDepthCompare(shadowMap, uv + vec2(dx1, dy0), depth) +    
    //     _czm_shadowDepthCompare(shadowMap, uv + vec2(dx0, 0.0), depth) +    
    //     _czm_shadowDepthCompare(shadowMap, uv + vec2(dx1, 0.0), depth) +    
    //     _czm_shadowDepthCompare(shadowMap, uv + vec2(dx0, dy1), depth) +    
    //     _czm_shadowDepthCompare(shadowMap, uv + vec2(0.0, dy1), depth) +    
    //     _czm_shadowDepthCompare(shadowMap, uv + vec2(dx1, dy1), depth)    
    //     ) * (1.0 / 9.0) ;    
        float visibility =_czm_shadowDepthCompare(shadowMap,uv,depth);
        return visibility;
}

vec3 pointProjectOnPlane(in vec3 planeNormal, in vec3 planeOrigin, in vec3 point){    
    vec3 v01 = point -planeOrigin;    
    float d = dot(planeNormal, v01) ;    
    return (point - planeNormal * d);
}

float ptm(vec3 pt){    
    return sqrt(pt.x*pt.x + pt.y*pt.y + pt.z*pt.z);
}

void main() {     
    const float PI = 3.141592653589793;    
    vec4 color = texture2D(colorTexture, v_textureCoordinates);    
    vec4 currD = texture2D(depthTexture, v_textureCoordinates);   
    //如果深度大于一，返回
    if(currD.r>=1.0){        
        gl_FragColor = color;        
        return;    
    }        
    //获取深度
    float depth = getDepth(currD);
    //转换到视点相机的视图坐标系 
    vec4 positionEC = toEye(v_textureCoordinates, depth);    
    vec3 normalEC = vec3(1.0);                                
    czm_shadowParameters shadowParameters;     
    shadowParameters.texelStepSize = shadowMap_texelSizeDepthBiasAndNormalShadingSmooth.xy;     
    shadowParameters.depthBias = shadowMap_texelSizeDepthBiasAndNormalShadingSmooth.z;     
    shadowParameters.normalShadingSmooth = shadowMap_texelSizeDepthBiasAndNormalShadingSmooth.w;     
    shadowParameters.darkness = shadowMap_normalOffsetScaleDistanceMaxDistanceAndDarkness.w;     
    shadowParameters.depthBias *= max(depth * 0.01, 1.0);     
    // 从顶点指向投影相机
    vec3 directionEC = normalize(positionEC.xyz - shadowMap_lightPositionEC.xyz);     
    // 得到角度，（0，1）之内
    float nDotL = clamp(dot(normalEC, -directionEC), 0.0, 1.0);
    //变换到投影相机的ndc坐标系，还未归一化    
    vec4 shadowPosition = _shadowMap_matrix * positionEC;     
    //进行归一化
    shadowPosition /= shadowPosition.w;     
    // 裁剪超出投影相机视锥体范围内的
    if (any(lessThan(shadowPosition.xyz, vec3(0.0))) || any(greaterThan(shadowPosition.xyz, vec3(1.0))))     
    {         
        gl_FragColor = color;        
        return;    
    }    
    // 纹理坐标
    shadowParameters.texCoords = shadowPosition.xy;
    //深度值  
    shadowParameters.depth = shadowPosition.z;
    // 投影光线角度 
    shadowParameters.nDotL = nDotL;     
    float visibility = _czm_shadowVisibility(stcshadow, shadowParameters);     
    vec4 videoColor = texture2D(videoTexture,shadowPosition.xy);    
    // 被遮挡
    if(visibility==1.0){   
            //赋值视频纹理  
        gl_FragColor = mix(color,vec4(videoColor.xyz,1.0),mixNum*videoColor.a);    
    }else{   
        //没有被遮挡但是超出近截面  
        if(abs(shadowPosition.z-0.0)<0.01){            
        return;        
        }       
    //     // 其他地方赋值原本颜色
    gl_FragColor = color;    
    }  
} 
`
